
cc.Class({
    extends: cc.Component,
    

    properties: {
        cow_Prefab:{
            default:null,
            type:cc.Prefab
        },
        rope_cow:{
            default:[],
            type:cc.SpriteFrame
        },
        score_Num:{
            default:null,
            type:cc.Label
        },
        gameOver:{
            default:null,
            type:cc.Prefab
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.cowNode = cc.instantiate(this.cow_Prefab);
        this.node.addChild(this.cowNode);
        this.rope_node = this.node.getChildByName("rope");
        this.rope_node.active = false;
        this.cowNode.on(cc.Node.EventType.TOUCH_END,this.clickCapture,this);
        this.NumScore = 0; 
        
        this.gameOverNode = cc.instantiate(this.gameOver);  
        this.gameOverNode.active = false;   
        this.node.addChild(this.gameOverNode);
        
        
    },

    start () {
        let TimeDown = this.node.getChildByName('Time').getComponent(cc.Label);
        let time = 10;
        this.schedule(function(){
            time --;
            TimeDown.string = "Time:" + time + 's';

            if(time <= 0){
                //游戏结束                
                let lastScore = this.gameOverNode.getChildByName("LastScore");
                lastScore.getComponent(cc.Label).string = "最终得分: " + this.NumScore;

                let level = this.gameOverNode.getChildByName("Level");
                switch(true){
                    case this.NumScore >= 0 && this.NumScore <= 2:
                        level.getComponent(cc.Label).string = "套牛青铜";
                        break;
                    case this.NumScore >= 3 && this.NumScore <= 5:
                        level.getComponent(cc.Label).string = "套牛星耀";
                        break;
                    case this.NumScore >6:
                        level.getComponent(cc.Label).string = "套牛王者";
                        break;
                }
                
                this.gameOverNode.active = true;
                this.gameOverNode.setSiblingIndex(102);
                cc.director.pause();
                this.node.getChildByName("capture_button").getComponent(cc.Button).interactable = false
                this.node.getChildByName("goStart").getComponent(cc.Button).interactable = false
                // this.node.destroy();

                cc.log("游戏结束");
                // cc.director.loadScene("gameOver");
            }
        },1);
    },
    update (dt) {
    },
    clickCapture(event, customEventData){
        
        this.rope_node.getComponent(cc.Sprite).spriteFrame = this.rope_cow[0];
        this.rope_node.active = true;
        //设置在父节点顺序
        this.rope_node.setSiblingIndex(100);
        //rope初始位置
        this.rope_node.position = cc.v2(0,-480);
        //rope向上移动
        let up = cc.moveTo(0.5,cc.v2(0,60))
        let down = cc.moveTo(0.6,cc.v2(0,-480))

        let result = cc.callFunc(()=>{
            let currentX = this.cowNode.x;
            cc.log("currentX:",currentX);
            let number = this.cowNode.getComponent("cow").number
            if(currentX > -100 && currentX < 100){
                cc.log("牛被抓到")
                this.node.removeChild(this.cowNode);

                this.rope_node.getComponent(cc.Sprite).spriteFrame = this.rope_cow[number + 1];

                this.cowNode = cc.instantiate(this.cow_Prefab);
                this.node.addChild(this.cowNode);

                //分数
                this.NumScore += 1;
                let scoreLabel = this.node.getChildByName('ScoreNumber').getComponent(cc.Label);
                scoreLabel.string = "Score: " + this.NumScore;
                
            }else{
                cc.log("牛没被抓到");
                this.rope_node.getComponent(cc.Sprite).spriteFrame = this.rope_cow[0];
            }
        },this)
        let seq = cc.sequence(up,result,down);
        this.rope_node.runAction(seq);
        
    },
    click_goStart(event, customEventData){
        cc.director.loadScene("Frontgame");
        this.node.destroy();
    }


});
